home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / scheme.el < prev    next >
Lisp/Scheme  |  1996-07-12  |  20KB  |  516 lines

  1. ;;; scheme.el --- Scheme mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Bill Rozas <jinz@prep.ai.mit.edu>
  6. ;; Keywords: languages, lisp
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Adapted from Lisp mode by Bill Rozas, jinx@prep.
  28. ;; Initially a query replace of Lisp mode, except for the indentation 
  29. ;; of special forms.  Probably the code should be merged at some point 
  30. ;; so that there is sharing between both libraries.
  31.  
  32. ;;; Code:
  33.  
  34. (defvar scheme-mode-syntax-table nil "")
  35. (if (not scheme-mode-syntax-table)
  36.     (let ((i 0))
  37.       (setq scheme-mode-syntax-table (make-syntax-table))
  38.       (set-syntax-table scheme-mode-syntax-table)
  39.  
  40.       ;; Default is atom-constituent.
  41.       (while (< i 256)
  42.     (modify-syntax-entry i "_   ")
  43.     (setq i (1+ i)))
  44.  
  45.       ;; Word components.
  46.       (setq i ?0)
  47.       (while (<= i ?9)
  48.     (modify-syntax-entry i "w   ")
  49.     (setq i (1+ i)))
  50.       (setq i ?A)
  51.       (while (<= i ?Z)
  52.     (modify-syntax-entry i "w   ")
  53.     (setq i (1+ i)))
  54.       (setq i ?a)
  55.       (while (<= i ?z)
  56.     (modify-syntax-entry i "w   ")
  57.     (setq i (1+ i)))
  58.  
  59.       ;; Whitespace
  60.       (modify-syntax-entry ?\t "    ")
  61.       (modify-syntax-entry ?\n ">   ")
  62.       (modify-syntax-entry ?\f "    ")
  63.       (modify-syntax-entry ?\r "    ")
  64.       (modify-syntax-entry ?  "    ")
  65.  
  66.       ;; These characters are delimiters but otherwise undefined.
  67.       ;; Brackets and braces balance for editing convenience.
  68.       (modify-syntax-entry ?[ "(]  ")
  69.       (modify-syntax-entry ?] ")[  ")
  70.       (modify-syntax-entry ?{ "(}  ")
  71.       (modify-syntax-entry ?} "){  ")
  72.       (modify-syntax-entry ?\| "  23")
  73.  
  74.       ;; Other atom delimiters
  75.       (modify-syntax-entry ?\( "()  ")
  76.       (modify-syntax-entry ?\) ")(  ")
  77.       (modify-syntax-entry ?\; "<   ")
  78.       (modify-syntax-entry ?\" "\"    ")
  79.       (modify-syntax-entry ?' "  p")
  80.       (modify-syntax-entry ?` "  p")
  81.  
  82.       ;; Special characters
  83.       (modify-syntax-entry ?, "_ p")
  84.       (modify-syntax-entry ?@ "_ p")
  85.       (modify-syntax-entry ?# "_ p14")
  86.       (modify-syntax-entry ?\\ "\\   ")))
  87.  
  88. (defvar scheme-mode-abbrev-table nil "")
  89. (define-abbrev-table 'scheme-mode-abbrev-table ())
  90.  
  91. (defun scheme-mode-variables ()
  92.   (set-syntax-table scheme-mode-syntax-table)
  93.   (setq local-abbrev-table scheme-mode-abbrev-table)
  94.   (make-local-variable 'paragraph-start)
  95.   (setq paragraph-start (concat "$\\|" page-delimiter))
  96.   (make-local-variable 'paragraph-separate)
  97.   (setq paragraph-separate paragraph-start)
  98.   (make-local-variable 'paragraph-ignore-fill-prefix)
  99.   (setq paragraph-ignore-fill-prefix t)
  100.   (make-local-variable 'indent-line-function)
  101.   (setq indent-line-function 'scheme-indent-line)
  102.   (make-local-variable 'parse-sexp-ignore-comments)
  103.   (setq parse-sexp-ignore-comments t)
  104.   (make-local-variable 'comment-start)
  105.   (setq comment-start ";")
  106.   (make-local-variable 'comment-start-skip)
  107.   ;; Look within the line for a ; following an even number of backslashes
  108.   ;; after either a non-backslash or the line beginning.
  109.   (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+[ \t]*")
  110.   (make-local-variable 'comment-column)
  111.   (setq comment-column 40)
  112.   (make-local-variable 'comment-indent-function)
  113.   (setq comment-indent-function 'scheme-comment-indent)
  114.   (make-local-variable 'parse-sexp-ignore-comments)
  115.   (setq parse-sexp-ignore-comments t)
  116.   (setq mode-line-process '("" scheme-mode-line-process)))
  117.  
  118. (defvar scheme-mode-line-process "")
  119.  
  120. (defun scheme-mode-commands (map)
  121.   (define-key map "\t" 'scheme-indent-line)
  122.   (define-key map "\177" 'backward-delete-char-untabify)
  123.   (define-key map "\e\C-q" 'scheme-indent-sexp))
  124.  
  125. (defvar scheme-mode-map nil)
  126. (if (not scheme-mode-map)
  127.     (progn
  128.       (setq scheme-mode-map (make-sparse-keymap))
  129.       (scheme-mode-commands scheme-mode-map)))
  130.  
  131. ;;;###autoload
  132. (defun scheme-mode ()
  133.   "Major mode for editing Scheme code.
  134. Editing commands are similar to those of lisp-mode.
  135.  
  136. In addition, if an inferior Scheme process is running, some additional
  137. commands will be defined, for evaluating expressions and controlling
  138. the interpreter, and the state of the process will be displayed in the
  139. modeline of all Scheme buffers.  The names of commands that interact
  140. with the Scheme process start with \"xscheme-\".  For more information
  141. see the documentation for xscheme-interaction-mode.
  142.  
  143. Commands:
  144. Delete converts tabs to spaces as it moves back.
  145. Blank lines separate paragraphs.  Semicolons start comments.
  146. \\{scheme-mode-map}
  147. Entry to this mode calls the value of scheme-mode-hook
  148. if that value is non-nil."
  149.   (interactive)
  150.   (kill-all-local-variables)
  151.   (scheme-mode-initialize)
  152.   (scheme-mode-variables)
  153.   (run-hooks 'scheme-mode-hook))
  154.  
  155. (defun scheme-mode-initialize ()
  156.   (use-local-map scheme-mode-map)
  157.   (setq major-mode 'scheme-mode)
  158.   (setq mode-name "Scheme"))
  159.  
  160. (defvar scheme-mit-dialect t
  161.   "If non-nil, scheme mode is specialized for MIT Scheme.
  162. Set this to nil if you normally use another dialect.")
  163.  
  164. (defun scheme-comment-indent (&optional pos)
  165.   (save-excursion
  166.     (if pos (goto-char pos))
  167.     (cond ((looking-at ";;;") (current-column))
  168.       ((looking-at ";;")
  169.        (let ((tem (calculate-scheme-indent)))
  170.          (if (listp tem) (car tem) tem)))
  171.       (t
  172.        (skip-chars-backward " \t")
  173.        (max (if (bolp) 0 (1+ (current-column)))
  174.         comment-column)))))
  175.  
  176. (defvar scheme-indent-offset nil "")
  177. (defvar scheme-indent-function 'scheme-indent-function "")
  178.  
  179. (defun scheme-indent-line (&optional whole-exp)
  180.   "Indent current line as Scheme code.
  181. With argument, indent any additional lines of the same expression
  182. rigidly along with this one."
  183.   (interactive "P")
  184.   (let ((indent (calculate-scheme-indent)) shift-amt beg end
  185.     (pos (- (point-max) (point))))
  186.     (beginning-of-line)
  187.     (setq beg (point))
  188.     (skip-chars-forward " \t")
  189.     (if (looking-at "[ \t]*;;;")
  190.     ;; Don't alter indentation of a ;;; comment line.
  191.     nil
  192.       (if (listp indent) (setq indent (car indent)))
  193.       (setq shift-amt (- indent (current-column)))
  194.       (if (zerop shift-amt)
  195.       nil
  196.     (delete-region beg (point))
  197.     (indent-to indent))
  198.       ;; If initial point was within line's indentation,
  199.       ;; position after the indentation.  Else stay at same point in text.
  200.       (if (> (- (point-max) pos) (point))
  201.       (goto-char (- (point-max) pos)))
  202.       ;; If desired, shift remaining lines of expression the same amount.
  203.       (and whole-exp (not (zerop shift-amt))
  204.        (save-excursion
  205.          (goto-char beg)
  206.          (forward-sexp 1)
  207.          (setq end (point))
  208.          (goto-char beg)
  209.          (forward-line 1)
  210.          (setq beg (point))
  211.          (> end beg))
  212.        (indent-code-rigidly beg end shift-amt)))))
  213.  
  214. (defun calculate-scheme-indent (&optional parse-start)
  215.   "Return appropriate indentation for current line as scheme code.
  216. In usual case returns an integer: the column to indent to.
  217. Can instead return a list, whose car is the column to indent to.
  218. This means that following lines at the same level of indentation
  219. should not necessarily be indented the same way.
  220. The second element of the list is the buffer position
  221. of the start of the containing expression."
  222.   (save-excursion
  223.     (beginning-of-line)
  224.     (let ((indent-point (point)) state paren-depth desired-indent (retry t)
  225.       last-sexp containing-se